﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.SystemManage
{
    public partial class HospitalAdd : BasePage
    {

        IBLL_Hospital ih = Factory.BLL_Hospital();

        public HospitalAdd()
        {
            this.ModuleNo = Module.系统管理.ToString();
            this.PageOperateType = OperateType.医疗机构管理.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            GetDefaultAreaString();
            if (!IsPostBack)
            {
                if (ih.GetNewHospitalID() != "QH")
                {
                    lblHospitalID.Text = ih.GetNewHospitalID();
                }
                else
                {
                    lblHospitalID.Text = "QH0001";
                }
            }

        }

      

        /// <summary>
        /// 获取地区查询条件 AreaID
        /// </summary>
        /// <returns></returns>
        private string GetAreaID()
        {
            string areaId = "";

            //首次进入使用当前用户所在区域ID
            areaId = CurrentAccount.GetUser().Org.Area.AreaID;

            //优先获取区县，其次城市，再次省份
            if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"]).String != Constants.ALL_ITEM_VALUE)
            {
                areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"];
            }
            else
            {
                if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlCity"]).String != Constants.ALL_ITEM_VALUE)
                {
                    areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlCity"];
                }
                else if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlProvice"]).String != Constants.ALL_ITEM_VALUE)
                {
                    areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlProvice"];
                }

            }
            return areaId;
        }

        /// <summary>
        /// 保存设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //获取医疗机构信息
            Hospital hospital = new Hospital();
            hospital.HospitalAddress = txtHospitalAddress.Text.Trim();
            hospital.ContactPerson = txtContactPerson.Text.Trim();
            hospital.ContactTel = txtContactTel.Text.Trim();
            Area area = new Area();
            hospital.HospitalID = lblHospitalID.Text.Trim().ToUpper(); ;
            hospital.HospitalName = txtHospitalName.Text.Trim();
            area.AreaID = GetAreaID();
            hospital.Area = area;
            hospital.AreaID = area.AreaID;



            #region 内蒙的


            //获取医疗机构信息
            Model.HospitalInfo hospitalInfo = new Model.HospitalInfo();
            hospitalInfo.HospitalID = hospital.HospitalID;
            hospitalInfo.HospitalName = hospital.HospitalName;
            hospitalInfo.Address = txtHospitalAddress.Text.Trim();
            hospitalInfo.ContactPerson = txtContactPerson.Text.Trim();
            hospitalInfo.TelNo = txtContactTel.Text.Trim();
            hospitalInfo.CellNo = txtMobile.Text.Trim();
            hospitalInfo.Email = txtEmail.Text.Trim();
            hospitalInfo.Levels = this.dplLevel.SelectedValue;
            hospitalInfo.AreaID = GetAreaID();


            hospitalInfo.LevelDetail = dplLevelDetail.SelectedValue;
            hospitalInfo.Code = txtCode.Text;

            hospitalInfo.Nature = ddlNature.SelectedValue;
            hospitalInfo.EconomicType = ddlEconomicType.SelectedValue;
            hospitalInfo.Organizers = ddlOrganizers.SelectedValue;
            hospitalInfo.Affiliation = ddlAffiliation.SelectedValue;

            hospitalInfo.Beds = txtBeds.Text;
            hospitalInfo.IsCooperative = rblistCooperative.SelectedValue == "1" ? true : false;
            hospitalInfo.IsInsurance = rblistInsurance.SelectedValue == "1" ? true : false;
            hospitalInfo.Expenditure = txtExpenditure.Text;
            hospitalInfo.IsOld = rblistOld.SelectedValue == "1" ? true : false;

            #endregion

            if (string.IsNullOrEmpty(lblHospitalID.Text.Trim()))
            {
                HZ.Web.WebUtil.Alert("医疗机构编号不能为空！", this);
                lblHospitalID.Focus();
                return;
            }

            if (string.IsNullOrEmpty(hospital.HospitalName))
            {
                HZ.Web.WebUtil.Alert("医疗机构名称不能为空！", this);
                txtHospitalName.Focus();
                return;
            }

            if (string.IsNullOrEmpty(hospital.Area.AreaID))
            {
                HZ.Web.WebUtil.Alert("请选择医疗机构所在的地区", this);
                ddlCity.Focus();
                return;
            }

            //保存设置

            int intFlag = ih.RegNewHospital(hospital, hospitalInfo);//1成功2医疗机构名称存在3医疗机构编号存在0失败
            if (intFlag == 1)
            {
                PageInfo("保存成功！", this.ReturnUrl);
            }
            else if (intFlag == 2)
            {
                HZ.Web.WebUtil.Alert("医疗机构名称已经存在", this);
                txtHospitalName.Focus();
                return;
            }
            else if (intFlag == 3)
            {
                lblHospitalID.Text = ih.GetNewHospitalID();

                HZ.Web.WebUtil.Alert("医疗机构编号已经存在,系统已重新获取新编号，请重新保存", this);
                return;
            }
            else
            {
                PageError("保存失败！", this.ReturnUrl);
            }
        }

        /// <summary>
        /// 获取默认选中的地区编号串
        /// 供JS函数使用
        /// </summary>
        /// <returns></returns>
        protected string GetDefaultAreaString()
        {
            //默认城市 江苏省 320000
            string defaultProvince = "150000";
            string defaultCity = "";
            //
            string strArea = "";

            //获取当前用户所在区域
            IBLL_Area areaCtrl = Factory.BLL_Area();
            Area area = areaCtrl.GetArea(CurrentAccount.GetUser().Org.Area.AreaID);

            //如果是市级中心机构，则默认选中具体城市
            if (area.FatherAreaID != "0")
            {
                defaultProvince = area.FatherAreaID;
                defaultCity = area.AreaID;
            }
            else
            {
                defaultProvince = area.AreaID;
            }

            //显示默认地区，范围之外的内容禁止选择
            strArea += "," + "\"" + defaultProvince + "\"";
            this.ddlProvice.Enabled = false;
            if (!string.IsNullOrEmpty(defaultCity))
            {
                strArea += "," + "\"" + defaultCity + "\"";
                this.ddlCity.Enabled = false;
            }
            else if (!string.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$ddlCity"]))
            {
                strArea += "," + "\"" + Request.Form["ctl00$ContentPlaceHolder1$ddlCity"] + "\"";
            }

            if (!string.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"]))
            {
                strArea += "," + "\"" + Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"] + "\"";
            }

            return strArea;
        }
    }
}
